package cn.huapu.power.energy.service.impl;

import cn.huapu.power.energy.bean.PeaFeeoptimize;
import cn.huapu.power.energy.bean.PeaUserpowerfee;
import cn.huapu.power.energy.bean.SysClient;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.bean.vo.PeaUserpowerfeeVo;
import cn.huapu.power.energy.ex.ErrorCodesException;
import cn.huapu.power.energy.mapper.PeaFeeoptimizeMapper;
import cn.huapu.power.energy.mapper.PeaUserpowerfeeMapper;
import cn.huapu.power.energy.mapper.SysClientMapper;
import cn.huapu.power.energy.service.PeaUserpowerfeeService;
import cn.huapu.power.energy.util.ObjectHelper;
import cn.huapu.power.energy.util.ValidateUtil;
import cn.huapu.power.energy.vo.Grid;
import cn.huapu.power.energy.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试用户权限服务实现类
 *
 * @author autogen
 */
@Service
public class PeaUserpowerfeeServiceImpl extends BaseServiceImpl<PeaUserpowerfeeMapper, PeaUserpowerfee, Integer> implements PeaUserpowerfeeService {
    @Autowired
    private PeaUserpowerfeeMapper feeMapper;
    @Autowired
    private PeaFeeoptimizeMapper zeMapper;
    @Autowired
    private SysClientMapper sysClientMapper;


    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> baseInfo(DateParameterBo da) {
        //功率因数信息
        Map<String, Object> map = new LinkedHashMap<>();
        PeaUserpowerfee fee = feeMapper.baseInfo(da);
        //行业标准
        Map<String, Object> m = feeMapper.hybzInfo(da);
        //实际功率因数第一名
        Map<String, Object> m1 = feeMapper.sjgvFirst();

        if (ObjectHelper.isNotEmpty(fee) && !ValidateUtil.mapIsNull(m)) {
            fee.setHybzInfo((double) m.get("ratioVal"));
            fee.setSjcosFirst((double) m1.get("sjCos"));
        }

        //优化前后对比图(调整前)
        da.setType1("功率因数");
        da.setType2("实际cos");
        PeaFeeoptimize timize = zeMapper.optionContrast(da);
        //优化前后对比图(调整后)
        da.setType2("调整后cos");
        PeaFeeoptimize timize1 = zeMapper.optionContrast(da);
        List<PeaFeeoptimize> listYh = new ArrayList<>();
        if (ObjectHelper.isNotEmpty(timize)) {
            listYh.add(timize);
        }
        if (ObjectHelper.isNotEmpty(timize1)) {
            listYh.add(timize1);
        }
        //提升功率因数方式(暂时写死)
        String s = "电力电容器补偿,合适的变压器容量,均衡用电负荷,合适的电动机,调整生产班次,提升用电负荷率";

        map.put("baseInfo", fee);
        map.put("listYh", listYh);
        map.put("tsfs", s);
        return map;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    @Override
    public Map<String, Object> feesCollection(PeaUserpowerfee bo) {
        Integer pkid = null;
        //新增单位
        SysClient client = new SysClient();
        client.setFullname(bo.getClientname());
        List<SysClient> list1 = sysClientMapper.list(client);
        if (!ValidateUtil.listIsNull(list1)) {
            pkid = list1.get(0).getPkid();
        } else {
            String clientname = bo.getClientname();
            if (clientname.length() > 2) {
                clientname = "**" + clientname.substring(2);
            }
            client.setClientnumber(bo.getClientnumber());
            client.setName(clientname);
            client.setFullname(bo.getClientname());
            client.setGvarid("500000000000");
            client.setClientnumber(bo.getClientnumber());
            sysClientMapper.insert(client);
            pkid = client.getPkid();
        }
        if (pkid == null) {
            throw new ErrorCodesException("单位信息出错", ErrorCodesException.DATA_ERR);
        }
        //新增用户电费记录表
        bo.setClientid(pkid);
//        bo.setClientname(client.getName());
        if (bo.getVollevel() == null) {
            bo.setVollevel(10);
        }
        if (bo.getIdstytype() == null) {
            bo.setIdstytype("C");
        }
        int insert = feeMapper.insertBo(bo);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("pkid", bo.getPkid());
        return map;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    public int edit(PeaUserpowerfee peaUserpowerfee) {
        return feeMapper.edit(peaUserpowerfee);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    public int add(PeaUserpowerfee peaUserpowerfee) {
        return feeMapper.add(peaUserpowerfee);
    }

    @Override
    @Transactional(readOnly = true)
    public Grid<PeaUserpowerfeeVo> queryByPage(Page pa, PeaUserpowerfeeVo peaUserpowerfeeVo) {
        List<PeaUserpowerfeeVo> peaUserpowerfeeVos = mapper.queryByPage(pa, peaUserpowerfeeVo);
        Grid<PeaUserpowerfeeVo> grid = new Grid<>();
        grid.setTotal(pa.getTotalCount());
        grid.setRows(peaUserpowerfeeVos);
        return grid;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    public boolean powfeeEdit(PeaUserpowerfee da) {
        int i = feeMapper.updatePowfee(da);
        return true;
    }
}
